package com.xsz.service;


import com.xsz.entity.Possition;
import com.xsz.entity.User;
import com.xsz.repository.PossitionRepository;
import com.xsz.util.KeyUtil;
import javafx.scene.Parent;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.subject.Subject;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.data.domain.Sort;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.Date;
import java.util.List;

@Service

public class PossitionService {
	@Resource
	PossitionRepository possitionRepository;

	//新建
	public Possition add(Possition possition) {
		possition.setId(KeyUtil.getId());
//		Subject subject = SecurityUtils.getSubject();
//		possition.setCreateBy(subject.getSession().getId().toString());
		possition.setCreateDate(new Date());
		User user = (User)SecurityUtils.getSubject().getPrincipal();
		possition.setCreateBy(user.getId());
		return possitionRepository.save(possition);
	}
	
	//修改

	public Possition update(Possition possition) {
		return possitionRepository.save(possition);
	}
	
	
	//删除

	public void delete(String id) {
		 possitionRepository.deleteById(id);
	}
	
	//查询

	public Possition selectById(String id) {
		 return possitionRepository.findById(id).get();
	}
	

//	public List<Possition> getAll() {
//		return possitionRepository.findAll();
//	}
	//查询分页
	public Page<Possition> getAll(String page, String limit) {
		Sort sort = new Sort(Sort.Direction.DESC, "createDate");
		Pageable pageable = PageRequest.of(Integer.parseInt(page), Integer.parseInt(limit),sort);
		return possitionRepository.findAll(pageable);
	}

	//根据登录用户id查询分页
	public Page<Possition> getAllById(String page, String limit) {
		Sort sort = new Sort(Sort.Direction.DESC, "createDate");
		Pageable pageable = PageRequest.of(Integer.parseInt(page), Integer.parseInt(limit),sort);
		User user = (User)SecurityUtils.getSubject().getPrincipal();
		return possitionRepository.findByCreateBy(pageable,user.getId());
	}

}
